﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Outlook;

namespace WhiteBook
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Whitepages.API.APIKey = Program.WhitePagesAPIKey;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Whitepages.wp result = null;
            if (Whitepages.API.ReversePhone("6043382512", out result))
            {
            }
        }
        private string GetPhone(string phone)
        {
            if (phone == null) return phone;
            phone = phone.Trim();
            if (phone == "") return phone;
            phone = System.Text.RegularExpressions.Regex.Replace(phone, @"[\-\[\[()\+ ]", "");
            if (phone.StartsWith("1")) phone = phone.Substring(1);
            if (phone.StartsWith("+1")) phone = phone.Substring(1);
            if (phone.StartsWith("6044843797"))
            {
                phone = phone.Substring(phone.LastIndexOf("p") + 1);
            }
            if (phone.ToLower().Contains("p"))
            {
                phone = phone.ToLower();
                phone = phone.Substring(0, phone.IndexOf("p"));
            }

            if (phone.ToLower().Contains("x"))
            {
                phone = phone.ToLower();
                phone = phone.Substring(0, phone.IndexOf("x"));
            }
            if (phone.Contains("#"))
            {
                string f = "";
            }
            phone = System.Text.RegularExpressions.Regex.Replace(phone, @"[A-Za-z#]", "");
            if (phone.StartsWith("011")) return "";
            return phone.Trim();
        }
        private void UpdateWork(ContactItem c)
        {
            if (c.BusinessAddress == null || c.BusinessAddress.Trim() == "")
            {
                if (c.BusinessTelephoneNumber != null && c.BusinessTelephoneNumber.Trim() != "")
                {
                    Whitepages.wp result = null;
                    string phone = GetPhone(c.BusinessTelephoneNumber);
                    if (phone != null && phone != "")
                    {
                        if (Whitepages.API.ReversePhone(phone, out result))
                        {
                            if (result.result.code == Whitepages.responsecode.FoundData)
                            {
                                Whitepages.address addy = result.listings[0].address;
                                c.BusinessAddress = addy.street;
                                c.BusinessAddressCity = addy.city;
                                c.BusinessAddressCountry = addy.country.ToString();
                                c.BusinessAddressPostalCode = addy.zip;
                                c.BusinessAddressState = addy.state;
                                c.BusinessAddressStreet = addy.street;
                            }
                        }

                    }
                }
            }
        }
        private void UpdateHome(ContactItem c)
        {
            if (c.HomeAddress == null || c.HomeAddress.Trim() == "")
            {
                if (c.HomeTelephoneNumber != null && c.HomeTelephoneNumber.Trim() != "")
                {
                    Whitepages.wp result = null;
                    string phone = GetPhone(c.HomeTelephoneNumber);
                    if (phone != null && phone != "")
                    {
                        if (Whitepages.API.ReversePhone(phone, out result))
                        {
                            if (result.result.code == Whitepages.responsecode.FoundData)
                            {
                                Whitepages.address addy = result.listings[0].address;
                                c.HomeAddress = addy.street;
                                c.HomeAddressCity = addy.city;
                                c.HomeAddressCountry = addy.country.ToString();
                                c.HomeAddressPostalCode = addy.zip;
                                c.HomeAddressState = addy.state;
                                c.HomeAddressStreet = addy.street;
                            }
                        }

                    }
                }
            }
        }
        private void UpdateMobile(ContactItem c)
        {
            if (c.HomeAddress == null || c.HomeAddress.Trim() == "")
            {
                if (c.MobileTelephoneNumber != null && c.MobileTelephoneNumber.Trim() != "")
                {
                    Whitepages.wp result = null;
                    string phone = GetPhone(c.MobileTelephoneNumber);
                    if (phone != null && phone != "")
                    {
                        if (Whitepages.API.ReversePhone(phone, out result))
                        {
                            if (result.result.code == Whitepages.responsecode.FoundData)
                            {
                                Whitepages.address addy = result.listings[0].address;
                                c.HomeAddress = addy.street;
                                c.HomeAddressCity = addy.city;
                                c.HomeAddressCountry = addy.country.ToString();
                                c.HomeAddressPostalCode = addy.zip;
                                c.HomeAddressState = addy.state;
                                c.HomeAddressStreet = addy.street;
                            }
                        }

                    }
                }
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            if (System.Windows.Forms.MessageBox.Show("Are you sure you want to update your address book?  This operation can not be undone!", "Confirm", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                List<ContactItem> contacts = Outlook.API.ReadContacts();
                this.TotalLabel.Text = contacts.Count.ToString();
                int x = 1;
                foreach (ContactItem c in contacts)
                {
                    this.CurrentLabel.Text = x.ToString();
                    this.NameLabel.Text = c.FirstName + " " + c.LastName;
                    System.Windows.Forms.Application.DoEvents();
                    UpdateHome(c);
                    UpdateWork(c);
                    UpdateMobile(c);
                    c.Save();
                    x++;
                }
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            Whitepages.wp result = null;
            if (Whitepages.API.FindPerson("William Gates", "WA", out result))
            {
                Whitepages.wp address = null;
                if (result.listings != null && result.listings.Length > 0)
                {
                    Whitepages.address addy = result.listings[0].address;
                    if (Whitepages.API.ReverseAddress(addy.house, addy.street, addy.city, addy.state, out address))
                    {

                    }


                    Whitepages.phone phone = null;
                    foreach (Whitepages.listing lst in result.listings)
                    {
                        if (lst.phonenumbers != null && lst.phonenumbers.Length > 0)
                            phone = lst.phonenumbers[0];
                    }
                    if (phone != null)
                    {
                        Whitepages.wp phoneResult = null;
                        Whitepages.API.ReversePhone(phone.fullphone, out phoneResult);
                    }
                }
                
            }

            
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Whitepages.wp business = null;
            if (Whitepages.API.FindBusiness("Microsoft Corporation", "Redmond", "WA", out business))
            {

            }
        }

        private void splitContainer1_SplitterMoved(object sender, SplitterEventArgs e)
        {

        }
    }
}
